package com.csw.shuanfa.GlobalConfig.format;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.jackson.JsonComponent;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 【此方法仅仅用于返回格式化，如果是入参的话还要手动加注解特别是LocalDateTime】
 * 全局日期格式化 如果某个字段不使用该格式
 * 依旧可以使用 @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd") 修改某个字段的格式化信息，且@JsonFormat优先级高于@JsonComponent配置的格式类型
 *
 * LocalDateTime注意-单个时间接收
 * public Result<LocalDateTime> aa(@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime date) {
 * LocalDateTime注意-嵌套对象接收（两个注解是不一样的）
 *   @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
 *     private LocalDateTime date;
 *
 *     如果需要打印日志，拿到的参数里面含有T有两种解决方法(obj可以手单个LocalDateTime，也可以是单个实体包含LocalDateTime的对象)
 *     方法一
 *       String string = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat).toString();
 *       方法二
 *         SerializeConfig serializeConfig = new SerializeConfig();
 *         serializeConfig.put(LocalDateTime.class, new LocalDateTimeSerializer());
 *         String json = JSON.toJSONString(date, serializeConfig);
 *
 *
 *     private static class LocalDateTimeSerializer implements ObjectSerializer {
 *         private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 *
 *         @Override
 *         public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
 *             LocalDateTime dateTime = (LocalDateTime) object;
 *             String formattedDateTime = dateTime.format(formatter);
 *             serializer.write(formattedDateTime);
 *         }
 *     }
 *
 */
@JsonComponent
@Component
public class DateFormatConfig {


    private String pattern = "yyyy-MM-dd HH:mm:ss";

    /**
     * 类型全局时间格式化
     */
    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() {
        return builder -> {
            // long -> String
            builder.serializerByType(Long.TYPE, ToStringSerializer.instance);
            // Long -> String
            builder.serializerByType(Long.class, ToStringSerializer.instance);
            // Long -> String
            builder.serializerByType(BigDecimal.class, ToStringSerializer.instance);


            // long -> String
//            builder.serializerByType(Long.TYPE, new ConditionalLongSerializer());
//            // Long -> String
//            builder.serializerByType(Long.class, new ConditionalLongSerializer());
            // Long -> String

            //序列化
            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)));

        };
    }


}

